package com.scau.ums.controller;

import com.scau.ums.mapper.SelectTaskMapper;
import com.scau.ums.model.*;
import com.scau.ums.service.UserInfo;
import com.scau.ums.service.factory.UserInfoImpFactory;
import com.scau.ums.service.imp.info.InfoImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
public class maintainerEntrance {

    private UserInfoImpFactory userInfoImpFactory;
    private InfoImp infoImp;
    private SelectTaskMapper selectTaskMapper;

    @Autowired
    public maintainerEntrance(UserInfoImpFactory userInfoImpFactory, InfoImp infoImp, SelectTaskMapper selectTaskMapper) {
        this.userInfoImpFactory = userInfoImpFactory;
        this.infoImp = infoImp;
        this.selectTaskMapper = selectTaskMapper;
    }

    @RequestMapping(value = "/maintainerEntrance", method = RequestMethod.GET)
    public String toMaintainerEntrance(HttpServletRequest request) {

        //获取维修工的信息
        String id= (String) request.getSession().getAttribute("USER_ID");
        int type= (int) request.getSession().getAttribute("USER_TYPE");
        UserInfo maintainerInfo= userInfoImpFactory.getUserInfoImp(type);
        User maintainer=maintainerInfo.getUser(id);
        request.setAttribute("maintainer",maintainer);


        //获取维修工分配到的任务
        List<Map<String, Object>> maintainertask=selectTaskMapper.SelectTask(id,"3");
        request.setAttribute("maintainertask",maintainertask);

        //获取维修工已完成的任务
        List<Map<String, Object>> finishtask=selectTaskMapper.SelectTask(id,"4");
        request.setAttribute("finishtask",finishtask);

        //获取维修工的维修历史
        List<Map<String, Object>> repairhistory=selectTaskMapper.SelectInfo(id,"7");
        request.setAttribute("repairhistory",repairhistory);

        for(int i=0;i<repairhistory.size();i++){
            for (String key :repairhistory.get(i).keySet()) {
                System.out.println("map.get(" + key + ") = " + repairhistory.get(i).get(key));
            }
        }




        return "maintainerEntrance";
    }

    @RequestMapping("/UpdateState")
    @ResponseBody
    public int UpdateState(ModelMap map,String reportId,String state) {
        int successUpdate=selectTaskMapper.UpadateTaskState(reportId,state);

        return successUpdate;

    }
}
